<?php

namespace common\logics;

use Yii;
use common\helpers\Url;
use common\helpers\ArrayHelper;

/**
 * This is the model class for table "{{%webview_map}}".
 *
 * @property int $id
 * @property string $title 页面标题
 * @property int $model 模式
 * @property string $type Webview类型
 * @property string $path 导航路由或名称
 * @property string $params 请求绑定参数
 * @property string $mark 备注
 * @property int $status 状态
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 * @property int $deleted_at 删除时间
 */
class SiteMapLogic extends \common\models\WebviewMap {

    private $_data = [];

    public function bindParams($data) {
        $this->_data = $data;
    }

    public function getData() {
        $params = $this->getFormatParams();
        $data = ArrayHelper::merge($params, $this->_data);
        return !empty($data) ? $data : null;
    }

    public function fields() {
        $fields = parent::fields();
        if (Yii::$app->id == 'app-mpend') {
            return [
                'name',
                'params' => 'data',
            ];
        } elseif (YII_ENV_DEV) {
            $fields[] = 'name';
        }
        return $fields;
    }

    /**
     * 获取路由地址
     * @param integer|string $id
     * @param array $data
     * @return array
     */
    public static function findByData($id, array $data = []) {
        $condition = [];
        if (is_numeric($id)) {
            $condition['id'] = $id;
        } elseif (is_string($id)) {
            $condition['path'] = $id;
        } else {
            $condition = [
                'or',
                'id' => $id,
                'path' => $id,
            ];
        }
        $model = static::find()->active()->andWhere($condition)->one();
        if (!$model) {
            return null;
        }
        $model->bindParams($data);
        return $model->toArray();
    }

}
